package com.zsw.procure_system.controller;


import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zsw.procure_system.mapper.CategoryMapper;
import com.zsw.procure_system.mapper.ProjectMapper;
import com.zsw.procure_system.pojo.Category;
import com.zsw.procure_system.pojo.Project;
import com.zsw.procure_system.pojo.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @GetMapping("/projectList")
    public ApiResult<List<Project>> getProjectList(@RequestParam int page) {
        PageHelper.startPage(page, 10);
        List<Project> projects = projectMapper.selectList(null);
        Integer total = projectMapper.selectCount(null);
        ApiResult<List<Project>> list = new ApiResult<>();
        list.setResultValue(projects);
        list.setTotal(total);
        return list;

    }

    @GetMapping("/search")
    public ApiResult<List<Project>> search(@RequestParam String name, @RequestParam String date) {
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.like("title", name);

        List<Project> projects = projectMapper.selectList(projectQueryWrapper);
        Integer total = projects.size();
        ApiResult<List<Project>> list = ApiResult.ok(projects);
        list.setResultValue(projects);
        list.setTotal(total);

        return list;

    }

    @GetMapping("/deleteItemById")
    public ApiResult<List<Project>> deleteItemById(@RequestParam int id) {
        int i = projectMapper.deleteById(id);
        List<Project> projects = projectMapper.selectList(null);
        int total = projects.size();
        ApiResult<List<Project>> list = ApiResult.ok(projects);
        list.setResultValue(projects);
        list.setTotal(total);
        return list;
    }


    @GetMapping("/itemCategory/selectItemCategoryByParentId")
    public ApiResult<List<Category>> selectItemCategoryByParentId(@RequestParam int type) {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("type", type);
        List<Category> categoriesList = categoryMapper.selectList(categoryQueryWrapper);
        ApiResult<List<Category>> list = ApiResult.ok(categoriesList);
        return list;
    }

    @PostMapping("/item/insertTbItem")
    public ApiResult<Boolean> insertTbItem(@RequestBody Project Project) {
        System.out.println(Project);
        projectMapper.insert(Project);
        return ApiResult.ok();

    }

    @GetMapping("/batchDelete")
    public ApiResult<Boolean> batchDelete(@RequestParam String ids) {
        System.out.println(ids);
        List<Integer> idsList = Arrays.stream(ids.split(",")).map(e -> Integer.parseInt(e)).collect(Collectors.toList());
        projectMapper.deleteBatchIds(idsList);
        return ApiResult.ok();

    }

    @PostMapping("/item/updateTbItem")
    public ApiResult<Boolean> updateTbItem(@RequestBody Project Project) {
        System.out.println(Project);
        projectMapper.updateById(Project);
        return ApiResult.ok();

    }

    @GetMapping("/itemCategory")
    public ApiResult<List<Category>> itemCategory() {
        List<Category> categories = categoryMapper.selectList(null);
        return ApiResult.ok(categories);

    }


    @GetMapping("/itemCategory/insertItemCategory")
    public ApiResult<Boolean> insertItemCategory(@RequestParam int cid, @RequestParam String name) {
        Category category = new Category();
        category.setId(12112);
        category.setType(cid);
        category.setName(name);
        long time = new Date().getTime();
        category.setCid((int) time);
        categoryMapper.insert(category);
        return ApiResult.ok();
    }

    @GetMapping("/itemCategory/updateCategory")
    public ApiResult<Boolean> updateCategory(@RequestParam int id, @RequestParam String name) {
        Category category = categoryMapper.selectById(id);
        category.setName(name);
        categoryMapper.updateById(category);
        return ApiResult.ok();
    }

    @GetMapping("/content/deleteContentCategoryById")
    public ApiResult<Boolean> deleteContentCategoryById(@RequestParam int id) {
        int i = categoryMapper.deleteById(id);
        return ApiResult.ok();
    }

    @GetMapping("/itemCategory/insertCategory")
    public ApiResult<Boolean> insertCategory(@RequestParam String name) {
        Category category = new Category();
        category.setId(Integer.parseInt(getRandomNum(6)));
        category.setName(name);
        category.setType(1);
        category.setCid(Integer.parseInt(getRandomNum(6)));
        int i = categoryMapper.insert(category);
        return ApiResult.ok();
    }


    public String getRandomNum(int length) {
        // 定义随机数字集
        String digits = "0123456789";
        // 创建Random对象
        Random random = new Random();
        // 创建一个StringBuilder对象来存储随机生成的数字
        StringBuilder sb = new StringBuilder(length);

        // 生成指定长度的随机数字ID
        for (int i = 0; i < length; i++) {
            // 从数字集中随机选取一个数字
            char randomDigit = digits.charAt(random.nextInt(digits.length()));
            // 将选取的数字添加到StringBuilder中
            sb.append(randomDigit);
        }

        // 返回随机生成的数字ID
        return sb.toString();
    }



}
